package com.usf.jprovider.schema.selector.value.split;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlSeeAlso;

import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.schema.selector.value.AbstractValueSelector;

@XmlSeeAlso({SplitFirstValue.class,
			 SplitLastValue.class,
			 SplitRandomValue.class})
public abstract class AbstractSplitValue extends AbstractValueSelector {

	@XmlAttribute protected String separator;
	
	public AbstractSplitValue() {
		super();
		separator = "";
	}
	public String getSeparator() {
		return separator;
	}

	@Override
	public final String create(Environment env, JObjectNode node, String source) {
		String[] str = source.split(separator);
		int index = index(env, node, str.length);
		node.setValueIndex(index);
		return str[index].trim();
	}

	@Override
	public final boolean check(Environment env, JObjectNode node, String source, String value){
		String[] values = source.split(separator);
		int index = startIndex(env, node, values.length);
		int end = endIndex(env, node, values.length) + 1;
		if(ignoreCase) while(index < end && !values[index].trim().equals(value)) index++;
		else while(index < end && !values[index].trim().equalsIgnoreCase(value)) index++;
		node.setValueIndex(index);
		return index < values.length;
	}	
}
